//
//  LFLoginViewController.m
//  QingDaoCommunity
//
//  Created by 李学林 on 15/11/23.
//  Copyright © 2015年 teteTechnology. All rights reserved.
//

#import "LFLoginViewController.h"
#import "UMSocial.h"

@interface LFLoginViewController ()


@property (weak, nonatomic) IBOutlet UIView *userNameView;
@property (weak, nonatomic) IBOutlet UIImageView *loginIcon;
@property (weak, nonatomic) IBOutlet UITextField *userNameTextFiled;

@property (weak, nonatomic) IBOutlet UIView *passwordView;
@property (weak, nonatomic) IBOutlet UIImageView *passwordIcon;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;


@property (weak, nonatomic) IBOutlet UIButton *remmberPwdBtn;
@property (weak, nonatomic) IBOutlet UIButton *forgetPwd;

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@property (weak, nonatomic) IBOutlet UIButton *rigisterBtn;

- (IBAction)weiboLogin:(UIButton *)sender;

- (IBAction)rememberPwdClick:(UIButton *)sender;
@property (nonatomic,assign,getter=isrememberPwd) BOOL rememberPwd;

/**
 *  青岛自己的登录按钮点击事件
 */
- (IBAction)QDLogin:(UIButton *)sender;

@end

@implementation LFLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.titleLab.text = @"登录";
    self.rightBtn.hidden = YES;
    
    _userNameView.backgroundColor = Login_Light_Color;
    _userNameView.layer.cornerRadius = 5;
    _userNameView.clipsToBounds = YES;
    _loginIcon.backgroundColor = Login_Deep_Color;
    
    
    _passwordView.backgroundColor = Login_Light_Color;
    _passwordView.layer.cornerRadius = 5;
    _passwordView.clipsToBounds = YES;
    _passwordIcon.backgroundColor = Login_Deep_Color;
    

    [self setTextFieldLeftView:_userNameTextFiled];
    [self setTextFieldLeftView:_passwordTextField];

    [_remmberPwdBtn setTitleColor:Login_Light_Color forState:UIControlStateNormal];
    [_forgetPwd setTitleColor:Login_Light_Color forState:UIControlStateNormal];
    
    _loginBtn.backgroundColor = Login_Deep_Color;
    _loginBtn.layer.cornerRadius = 5;
    _loginBtn.clipsToBounds = YES;
//    _loginBtn.enabled = NO;
    [_rigisterBtn setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
    
}

/**
 *  解决textfiled左边间距的方法
 *
 *  @param textField 要解决textfile问题的对象
 */
- (void)setTextFieldLeftView:(UITextField *)textField{

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 1)];
    [textField addSubview:view];
    textField.leftView = view;
    textField.leftViewMode = UITextFieldViewModeAlways;

}

- (void)backMethod{

    [self.navigationController popToRootViewControllerAnimated:YES];

    AppDelegate *app =(AppDelegate *)[UIApplication sharedApplication].delegate;
    [app.tab showOrHiddenTabbarView:NO];

}



/**
 *  微博第三方登录
 *
 *  @param sender 微博点击按钮
 */
- (IBAction)weiboLogin:(UIButton *)sender {
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        // 获取微博用户名、uid、token等
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
        }});
}

- (IBAction)rememberPwdClick:(UIButton *)sender {
    
    if (self.isrememberPwd) {
        
        self.rememberPwd = NO;
        self.remmberPwdBtn.selected = NO;
        return;
    }else{
    
        self.rememberPwd = YES;
        self.remmberPwdBtn.selected = YES;
        return;
    }
    
}

- (void)QDLoginState{
    
    NSString *username = self.userNameTextFiled.text;
    NSString *password = self.passwordTextField.text;
    


    if (username.length == 0 || password.length == 0){

        // 弹窗 提示用户输入账号和密码
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"亲, 请输入用户名或密码" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"好的", nil];
        
        [alertView show];
        
    }else {
        
        // 进行正则判断@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        NSString *namePattern = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
        
        NSRegularExpression *reqularExpression = [NSRegularExpression regularExpressionWithPattern:namePattern options:0 error:NULL];
        
        NSTextCheckingResult *result = [reqularExpression firstMatchInString:username options:NSMatchingReportCompletion range:NSMakeRange(0, username.length)];
        
        NSString *resultUserName = [username substringWithRange:result.range];
        
        NSLog(@">>>>>>%@",resultUserName);
        
        
        if (resultUserName.length == 0) {
            
            // 弹窗 提示用户输入账号和密码
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"亲, 请用邮箱登录" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"好的", nil];
            
            [alertView show];
            
        }else{
        
            // 存储到本地
            if (self.rememberPwd) {
                
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"rememberPwd"];
                
                [[NSUserDefaults standardUserDefaults] setObject:username forKey:@"userName"];
                [[NSUserDefaults standardUserDefaults]synchronize];
                
                [[NSUserDefaults standardUserDefaults]setObject:resultUserName forKey:@"passWord"];
                [[NSUserDefaults standardUserDefaults]synchronize];
                
            }else{
            
                [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"rememberPwd"];
            
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userName"];
                [[NSUserDefaults standardUserDefaults]synchronize];
                
                [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"passWord"];
                [[NSUserDefaults standardUserDefaults]synchronize];
            }
            
            
            // 发布通知告诉基类右上角
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginSuccess" object:nil userInfo:nil];

            
            // 跳转到登录界面
            [self.navigationController popToRootViewControllerAnimated:YES];
        
        }
    }
}

- (IBAction)QDLogin:(UIButton *)sender {
    
    
    [self QDLoginState];
    
    
    
}
@end
